package javbot.gui;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import javbot.Constants;
import javbot.GlobalClass;
import javbot.Javbot;
import javbot.gui.actions.CmdClickAction;
import javbot.gui.actions.CmdGoToAction;
import javbot.gui.actions.CmdKeyAction;
import javbot.gui.actions.CmdMailScreenshotAction;
import javbot.gui.actions.CmdScreenshotAction;
import javbot.gui.actions.CmdTypeAction;
import javbot.gui.actions.CmdWaitAction;
import javbot.gui.actions.OptionsSendMailAction;
import javbot.script.Script;

public class JavbotFrame extends JFrame {

	private static final long serialVersionUID = -5707660748738548181L;

	private static final String JAVBOT = "Javbot";

	private JPanel jContentPane = null;

	private JMenuBar menuBar = null;

	private JMenu fileMenu = null;

	private JMenu editMenu = null;

	private JMenu commandMenu = null;	
	
	private JMenu helpMenu = null;

	private JMenuItem exitMenuItem = null;

	private JMenuItem aboutMenuItem = null;

	private JMenuItem cutMenuItem = null;

	private JMenuItem copyMenuItem = null;

	private JMenuItem pasteMenuItem = null;

	private JMenuItem saveMenuItem = null;

	public JavbotPanel javbotPanel = null;

	private JMenuItem cmdGoToMenuItem = null;
	
	private JMenuItem cmdWaitMenuItem = null;	

	private JMenuItem cmdJumpToMenuItem = null;

	private JMenuItem cmdClickMenuItem = null;

	private JToolBar toolBar = null;

	private JMenuItem mapKbrdMenuItem = null;

	private JMenuItem cmdTypeMenuItem = null;

	private JMenuItem sampleScriptMenuItem = null;

	private JMenu optionsMenu = null;

	private JMenuItem repeatMenuItem = null;

	private JMenuItem cmdScreenshotMenuItem = null;

	private JMenuItem cmdMailFilesMenuItem = null;

	private JMenuItem helpCommandsMenuItem = null;
	
	private JMenuItem helpAdvancedCommandsMenuItem = null;

	private JMenuItem mailCheckMenuItem = null;

	private JMenuItem sendMailMenuItem = null;
	
	private JMenu lookAndFeelMenu = null;	

	private JMenuItem cmdKey = null;

	private JCheckBoxMenuItem showConsoleMenuItem = null;
	
	private JCheckBoxMenuItem checkMailMenuItem = null;

	private ConsolePanel consolePanel = null;

	private JMenuItem executeMenuItem = null;

	private JMenuItem openMenuItem = null;
	
	private JMenuItem saveAsMenuItem = null;
	
	private File openedFile = null;

	private JMenuItem newMenuItem = null;

	private JMenu insertMenuItem = null;

	/**
	 * This is the default constructor
	 */
	public JavbotFrame() {
		super();
		initialize();
	}
	
	private void setOpenedFile(File openedFile) {
		if (openedFile != null) {
			saveMenuItem.setEnabled(true);
			setTitle(JAVBOT + " - " + openedFile.getName());
			
		} else {
			saveMenuItem.setEnabled(false);
			setTitle(JAVBOT);
		}
		
		this.openedFile = openedFile;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Constants.JAVBOT_IMAGE)));
		this.setJMenuBar(getJJMenuBar());
		this.setSize(272, 200);
		this.setContentPane(getJContentPane());
		this.setTitle(JAVBOT);
		this.pack();
		setOpenedFile(null);
		saveAsMenuItem.setEnabled(true);
		javbotPanel.scriptPanel.scriptInput.requestFocus();
	}
	
	private void setScript(String text) {
		javbotPanel.scriptPanel.scriptInput.setText(text);
		javbotPanel.scriptPanel.scriptInput.setCaretPosition(text.length());
		javbotPanel.scriptPanel.scriptInput.requestFocusInWindow();		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getScriptPanel(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getFileMenu());
			menuBar.add(getEditMenu());
			menuBar.add(getCommandMenu());
			menuBar.add(getOptionsMenu());
			menuBar.add(getHelpMenu());
		}
		return menuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getMapKbrdMenuItem());
			fileMenu.add(new JSeparator());
			fileMenu.add(getNewMenuItem());
			fileMenu.add(getOpenMenuItem());
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getSaveAsMenuItem());		
			fileMenu.add(new JSeparator());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}
	
	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getCommandMenu() {
		if (commandMenu == null) {
			commandMenu = new JMenu();
			commandMenu.setText("Command");
			commandMenu.add(getCmdGoToMenuItem());
			commandMenu.add(getCmdJumpToMenuItem());
			commandMenu.add(getCmdClickMenuItem());
			commandMenu.add(getCmdTypeMenuItem());
			commandMenu.add(getCmdWaitMenuItem());			
			commandMenu.add(getCmdKey());
			commandMenu.add(getExecuteMenuItem());			
			commandMenu.add(getCmdScreenshotMenuItem());
			commandMenu.add(getCmdMailScreenshotMenuItem());
			commandMenu.add(getInsertMenuItem());
			
		}
		return commandMenu;
	}	
	
	/**
	 * This method initializes jMenuRun	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getOptionsMenu() {
		if (optionsMenu == null) {
			optionsMenu = new JMenu();
			optionsMenu.setText("Options");
			optionsMenu.add(getRepeatMenuItem());
			optionsMenu.add(getMailCheckMenuItem());
			optionsMenu.add(getSendMailMenuItem());
			optionsMenu.add(new JSeparator());		
			optionsMenu.add(getLookAndFeelMenuItem());						
//			optionsMenu.add(new JSeparator());		
			optionsMenu.add(getShowConsoleMenuItem());
			optionsMenu.add(getCheckMailMenuItem());
		}
		return optionsMenu;
	}	

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getSampleScriptMenuItem());
			helpMenu.add(getHelpCommandsMenuItem());
			helpMenu.add(getHelpAdvancedCommandsMenuItem());
			helpMenu.add(new JSeparator());
			helpMenu.add(getAboutMenuItem());			
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About Javbot");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog d = new JDialog(JavbotFrame.this, "About", true);
					d.setSize(285,350);
					d.setContentPane(new AboutPanel());					
					d.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
					Event.CTRL_MASK, false));
			cutMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					javbotPanel.scriptPanel.scriptInput.cut();
				}
			});
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					Event.CTRL_MASK, false));
			copyMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					javbotPanel.scriptPanel.scriptInput.copy();
				}
			});
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					Event.CTRL_MASK, false));
			pasteMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					javbotPanel.scriptPanel.scriptInput.paste();
				}
			});
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
			saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Script.saveScript(openedFile, javbotPanel.scriptPanel.scriptInput.getText());
				}
			});
		}
		return saveMenuItem;
	}

	/**
	 * This method initializes scriptPanel	
	 * 	
	 * @return jabot.gui.ScriptPanel	
	 */
	private JavbotPanel getScriptPanel() {
		if (javbotPanel == null) {
			javbotPanel = new JavbotPanel();
			javbotPanel.add(getToolBar(), java.awt.BorderLayout.NORTH);
			javbotPanel.add(getConsolePanel(), java.awt.BorderLayout.EAST);
		}
		return javbotPanel;
	}

	/**
	 * This method initializes cmdGoToMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdGoToMenuItem() {
		if (cmdGoToMenuItem == null) {
			cmdGoToMenuItem = new JMenuItem();
			cmdGoToMenuItem.setText("Go To");
			cmdGoToMenuItem.addActionListener(new CmdGoToAction(this));
		}
		return cmdGoToMenuItem;
	}

	/**
	 * This method initializes cmdJumpToMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdJumpToMenuItem() {
		if (cmdJumpToMenuItem == null) {
			cmdJumpToMenuItem = new JMenuItem();
			cmdJumpToMenuItem.setText("Jump To");
			cmdJumpToMenuItem.addActionListener(new CmdJumpTo());			
		}
		return cmdJumpToMenuItem;
	}

	/**
	 * This method initializes cmdClickMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdClickMenuItem() {
		if (cmdClickMenuItem == null) {
			cmdClickMenuItem = new JMenuItem();
			cmdClickMenuItem.setText("Click");
			cmdClickMenuItem.addActionListener(new CmdClickAction(this));					
		}
		return cmdClickMenuItem;
	}

	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new Toolbar(this);
		}
		return toolBar;
		
	}
	
    private final class CmdJumpTo implements java.awt.event.ActionListener {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			javbotPanel.scriptPanel.insertJumpTo();
		}
	}

	/**
	 * This method initializes mapKbrdMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getMapKbrdMenuItem() {
		if (mapKbrdMenuItem == null) {
			mapKbrdMenuItem = new JMenuItem();
			mapKbrdMenuItem.setText("Map Keyboard");			
			mapKbrdMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Thread t = new Thread() {
						public void run() {
							try {
								GlobalClass.initKeyData(true);
							} catch (AWTException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
					};
					t.start();
				}
			});					
		}
		return mapKbrdMenuItem;
	}	
	
	/**
	 * This method initializes cmdTypeMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdTypeMenuItem() {
		if (cmdTypeMenuItem == null) {
			cmdTypeMenuItem = new JMenuItem();
			cmdTypeMenuItem.setText("Type");
			cmdTypeMenuItem.addActionListener(new CmdTypeAction(this));		

		}
		return cmdTypeMenuItem;
	}

	
	/**
	 * This method initializes cmdWaitMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdWaitMenuItem() {
		if (cmdWaitMenuItem == null) {
			cmdWaitMenuItem = new JMenuItem();
			cmdWaitMenuItem.setText("Wait");
			cmdWaitMenuItem.addActionListener(new CmdWaitAction(this));		

		}
		return cmdWaitMenuItem;
	}	
	
	/**
	 * This method initializes jSampleScriptMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSampleScriptMenuItem() {
		if (sampleScriptMenuItem == null) {
			sampleScriptMenuItem = new JMenuItem();
			sampleScriptMenuItem.setText("Sample Script");
			sampleScriptMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					javbotPanel.scriptPanel.insertSample();
				}
			});					
		}
		return sampleScriptMenuItem;
	}

	/**
	 * This method initializes repeatMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	
	private JMenuItem getRepeatMenuItem() {
		if (repeatMenuItem == null) {
			repeatMenuItem = new JMenuItem();
			repeatMenuItem.setText("Repeat...");
			
			repeatMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					showRepeatScriptDialog();
				}
			});
		}
		return repeatMenuItem;
	}
	
	public void showRepeatScriptDialog() {
        Object[] possibilities = {"Never", "Successively", 
        		"Every minute", "Every 5 minutes",  "Every 1/2 hour", "Every hour", 
        		"Every 6 hours", "Every 12 hours", "Every day", "Every week"};
        
        String s = (String)JOptionPane.showInputDialog(
                            this,
                            "Repeat script:\n",
                            "Repeat Option",
                            JOptionPane.PLAIN_MESSAGE,
                            null,
                            possibilities,
                            "Once");

        //If a string was returned, say so.
        if ((s != null) && (s.length() > 0)) {
        	for (int i=0; i < possibilities.length; i++) {
        		if (s.equals(possibilities[i]))
        			switch(i) {
        				case 0: GlobalClass.repeat = -1; break;
        				case 1: GlobalClass.repeat = 0; break;
        				case 2: GlobalClass.repeat = 60; break;
        				case 3: GlobalClass.repeat = 60*5; break;
        				case 4: GlobalClass.repeat = 60*30; break;
        				case 5: GlobalClass.repeat = 60*60; break;
        				case 6: GlobalClass.repeat = 60*60*6; break;
        				case 7: GlobalClass.repeat = 60*60*12; break;
        				case 8: GlobalClass.repeat = 60*60*24; break;
        				case 9: GlobalClass.repeat = 60*60*24*7; break;
        			}
        	}
        	System.out.println(s);

        } else {
        	System.out.println("Cancel");
        }
	}

	/**
	 * This method initializes cmdScreenshotMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdScreenshotMenuItem() {
		if (cmdScreenshotMenuItem == null) {
			cmdScreenshotMenuItem = new JMenuItem();
			cmdScreenshotMenuItem.setText("Screenshot");
			cmdScreenshotMenuItem.addActionListener(new CmdScreenshotAction(this));					
		}
		return cmdScreenshotMenuItem;
	}

	/**
	 * This method initializes cmdMailScreenshotMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdMailScreenshotMenuItem() {
		if (cmdMailFilesMenuItem == null) {
			cmdMailFilesMenuItem = new JMenuItem();
			cmdMailFilesMenuItem.setText("Mail Files");
			cmdMailFilesMenuItem.addActionListener(new CmdMailScreenshotAction(this));				
		}
		return cmdMailFilesMenuItem;
	}

	/**
	 * This method initializes helpMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getHelpCommandsMenuItem() {
		if (helpCommandsMenuItem == null) {
			helpCommandsMenuItem = new JMenuItem();
			helpCommandsMenuItem.setText("Commands");
			helpCommandsMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFrame frame = new JFrame("Help - Commands");
					frame.setSize(500,390);
					HtmlPanel hp = new HtmlPanel(Constants.HELP_COMMANDS);
					frame.setContentPane(hp);	
					frame.setVisible(true);
					hp.scrollToTop();
				}
			});
		}
		return helpCommandsMenuItem;
	}
	
	/**
	 * This method initializes helpMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getHelpAdvancedCommandsMenuItem() {
		if (helpAdvancedCommandsMenuItem == null) {
			helpAdvancedCommandsMenuItem = new JMenuItem();
			helpAdvancedCommandsMenuItem.setText("Advanced Commands");
			helpAdvancedCommandsMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFrame frame = new JFrame("Help - Advanced Commands");
					frame.setSize(500,390);
					HtmlPanel hp = new HtmlPanel(Constants.HELP_ADVANCED_COMMANDS);
					frame.setContentPane(hp);	
					frame.setVisible(true);
					hp.scrollToTop();
				}
			});
		}
		return helpAdvancedCommandsMenuItem;
	}	

	/**
	 * This method initializes mailCheckMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getMailCheckMenuItem() {
		if (mailCheckMenuItem == null) {
			mailCheckMenuItem = new JMenuItem();
			mailCheckMenuItem.setText("Mail Checker...");
			mailCheckMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog d = new JDialog(JavbotFrame.this, "Mail Checker", true);
					d.setSize(320,200);
					d.setContentPane(new MailCheckPanel());					
					d.setVisible(true);
				}
			});			
		}
		return mailCheckMenuItem;
	}

	/**
	 * This method initializes sendMailMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSendMailMenuItem() {
		if (sendMailMenuItem == null) {
			sendMailMenuItem = new JMenuItem();
			sendMailMenuItem.setText("Mail Sending...");
			sendMailMenuItem.addActionListener(new OptionsSendMailAction(this));					
		}
		return sendMailMenuItem;
	}

	public void updateLookAndFeel(String lafClassName) {
        if (lafClassName != null) {
            final String finalLafClassName = lafClassName;
            final JFrame frame = this;
            Runnable runnable = new Runnable() {
              public void run() {
                try {
                  UIManager.setLookAndFeel(finalLafClassName);
                  SwingUtilities.updateComponentTreeUI(frame);
                } catch (Exception exception) {
                  JOptionPane.showMessageDialog(frame,
                      "Can't change look and feel",
                      "Invalid Platform",
                      JOptionPane.ERROR_MESSAGE);
                }
              }
            };
            SwingUtilities.invokeLater(runnable);
          }
	}
	
	/**
	 * This method initializes lookAndFeelMenu
	 * 	
	 * @return javax.swing.JMenu
	 */
	private JMenu getLookAndFeelMenuItem() {
		if (lookAndFeelMenu == null) {
			lookAndFeelMenu = new JMenu();
			lookAndFeelMenu.setText("Look and Feel");			
		}


	    ActionListener actionListener = new ActionListener() {
	      public void actionPerformed(ActionEvent actionEvent) {
	        Object source = actionEvent.getSource();
	        if (source instanceof JMenuItem)
	        	 updateLookAndFeel(((JMenuItem) source).getName());	       
	      }
	    };

	    
	    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
		for (int i = 0, n = looks.length; i < n; i++) {
			JMenuItem menuItem = new JMenuItem(looks[i].getName());
			menuItem.setName(looks[i].getClassName());
			menuItem.addActionListener(actionListener);
			lookAndFeelMenu.add(menuItem);
		}
		
		return lookAndFeelMenu;
	}	
	
	/**
	 * This method initializes cmdKey	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCmdKey() {
		if (cmdKey == null) {
			cmdKey = new JMenuItem();
			cmdKey.setText("Key...");
			cmdKey.addActionListener(new CmdKeyAction(this));
		}
		return cmdKey;
	}

	/**
	 * This method initializes jcbmiShowConsole	
	 * 	
	 * @return javax.swing.JCheckBoxMenuItem	
	 */
	private JCheckBoxMenuItem getShowConsoleMenuItem() {
		if (showConsoleMenuItem == null) {
			showConsoleMenuItem = new JCheckBoxMenuItem();
			showConsoleMenuItem.setText("Show Console");
			
			showConsoleMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean selected = showConsoleMenuItem.isSelected();
					consolePanel.setVisible(selected);
					Dimension d = Javbot.javbotFrame.getSize();
					if (selected)
						d.width += ConsolePanel.CONSOLE_WIDTH;
					else 
						d.width -= ConsolePanel.CONSOLE_WIDTH;
					Javbot.javbotFrame.setSize(d);
					Javbot.javbotFrame.validate();
					
					
				}
			});					
		}
		return showConsoleMenuItem;
	}

	/**
	 * This method initializes jcbmiCheckMail	
	 * 	
	 * @return javax.swing.JCheckBoxMenuItem	
	 */
	private JCheckBoxMenuItem getCheckMailMenuItem() {
		if (checkMailMenuItem == null) {
			checkMailMenuItem = new JCheckBoxMenuItem();
			checkMailMenuItem.setText("Check Mail");
			
			checkMailMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean isSelected = checkMailMenuItem.isSelected();
					if (isSelected)
						GlobalClass.startMailChecker();
					else
						GlobalClass.stopMailChecker();
					
					commandMenu.setEnabled(!isSelected);
					javbotPanel.setEnabled(!isSelected);
					
					
				}
			});					
		}
		return checkMailMenuItem;
	}	
	
	/**
	 * This method initializes consolePanel	
	 * 	
	 * @return javbot.gui.ConsolePanel	
	 */
	private ConsolePanel getConsolePanel() {
		if (consolePanel == null) {
			consolePanel = new ConsolePanel();
			consolePanel.setVisible(false);
			GlobalClass.cPanel = consolePanel;
		}
		return consolePanel;
	}

	/**
	 * This method initializes executeMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExecuteMenuItem() {
		if (executeMenuItem == null) {
			executeMenuItem = new JMenuItem();
			executeMenuItem.setText("Execute");
			executeMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					javbotPanel.scriptPanel.insertExecute();
				}
			});					

			
		}
		return executeMenuItem;
	}

	/**
	 * This method initializes menuItemOpen	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getOpenMenuItem() {
		if (openMenuItem == null) {
			openMenuItem = new JMenuItem();
			openMenuItem.setText("Open...");
			openMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					  //Handle open button action.
					final JFileChooser fc = new JFileChooser();
				        int returnVal = fc.showOpenDialog((Component) e.getSource());

				        if (returnVal == JFileChooser.APPROVE_OPTION) {
				        	File file = fc.getSelectedFile();
				            if (file.exists()) {
				            	setScript(Script.readScript(file));
				            	setOpenedFile(file);				            	
				            }
				        } else {
				            
				        }
 
				}
			});				
			
		}
		return openMenuItem;
	}

	/**
	 * This method initializes saveAsMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSaveAsMenuItem() {
		if (saveAsMenuItem == null) {
			saveAsMenuItem = new JMenuItem();
			saveAsMenuItem.setText("Save As...");
			saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					  //Handle open button action.
					final JFileChooser fc = new JFileChooser();
				        int returnVal = fc.showSaveDialog((Component) e.getSource());
				        
				        if (returnVal == JFileChooser.APPROVE_OPTION) {
				        	File file = fc.getSelectedFile();
				        	Script.saveScript(file,javbotPanel.scriptPanel.scriptInput.getText());
				        	setOpenedFile(file);
				        } else {
				            
				        }
 
				}
			});							
		}
		return saveAsMenuItem;
	}

	/**
	 * This method initializes newMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getNewMenuItem() {
		if (newMenuItem == null) {
			newMenuItem = new JMenuItem();
			newMenuItem.setText("New");
			newMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setOpenedFile(null);	
					javbotPanel.scriptPanel.scriptInput.setText("");
				}
			});							
		}			
		return newMenuItem;
	}

	/**
	 * This method initializes insertMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getInsertMenuItem() {
		
		
		if (insertMenuItem == null) {

			insertMenuItem = new JMenu();
			insertMenuItem.setText("Insert");	
			
			ActionListener actionListener = new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (e.getActionCommand().equals("Maximize Window"))
						javbotPanel.scriptPanel.insertString("Key ALT, SPACE; Key X;");
					else if (e.getActionCommand().equals("Show Desktop"))
						javbotPanel.scriptPanel.insertString("Key WINKEY, D;");
					else if (e.getActionCommand().equals("Run"))
						javbotPanel.scriptPanel.insertString("Key WINKEY, R;");
					
					System.out.println(e.getActionCommand());
				}
			};
			
			JMenu insertWindowsMenuItem =  new JMenu();
			insertWindowsMenuItem.setText("MS Windows");
			insertMenuItem.add(insertWindowsMenuItem);
			
			
			JMenuItem mWindows1 = new JMenuItem("Maximize Window");
			mWindows1.addActionListener(actionListener);
			insertWindowsMenuItem.add(mWindows1);

			JMenuItem mWindows2 = new JMenuItem("Show Desktop");
			mWindows2.addActionListener(actionListener);
			insertWindowsMenuItem.add(mWindows2);

			JMenuItem mWindows3 = new JMenuItem("Run");
			mWindows3.addActionListener(actionListener);
			insertWindowsMenuItem.add(mWindows3);
		}
		return insertMenuItem;
	}
}  //  @jve:decl-index=0:visual-constraint="232,10"
